﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Core;
using EPiServer.Web.WebControls;
using System.Linq;
namespace EPiServer.Municipality.Web.UI.WebControls
{
    /// <summary>
    /// Summary description for PagedList.
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class EventPagedList : PageListData
    {
        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region public ITemplate HeaderTemplate
        /// <summary>
        /// Get/Sets the HeaderTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(EventPageTemplateContainer))]
        public ITemplate HeaderTemplate
        {
            get
            {
                return _headerTemplate;
            }
            set
            {
                _headerTemplate = value;
            }
        }
        private ITemplate _headerTemplate;
        #endregion
        #region public ITemplate ItemTemplate
        /// <summary>
        /// Get/Sets the ItemTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(EventPageTemplateContainer))]
        public ITemplate ItemTemplate
        {
            get
            {
                return _itemTemplate;
            }
            set
            {
                _itemTemplate = value;
            }
        }
        private ITemplate _itemTemplate;
        #endregion
        #region public ITemplate EventHeadingTemplate
        /// <summary>
        /// Get/Sets the EventHeadingTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(EventPageTemplateContainer))]
        public ITemplate EventHeadingTemplate
        {
            get
            {
                return _eventHeadingTemplate;
            }
            set
            {
                _eventHeadingTemplate = value;
            }
        }
        private ITemplate _eventHeadingTemplate;
        #endregion
        #region public ITemplate EmptyItemTemplate
        /// <summary>
        /// Get/Sets the EmptyItemTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(EventPageTemplateContainer))]
        public ITemplate EmptyItemTemplate
        {
            get
            {
                return _emptyItemTemplate;
            }
            set
            {
                _emptyItemTemplate = value;
            }
        }
        private ITemplate _emptyItemTemplate;
        #endregion
        #region public ITemplate FooterTemplate
        /// <summary>
        /// Get/Sets the FooterTemplate of the PagedNewsList
        /// </summary>
        /// <value></value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(EventPageTemplateContainer))]
        public ITemplate FooterTemplate
        {
            get
            {
                return _footerTemplate;
            }
            set
            {
                _footerTemplate = value;
            }
        }
        private ITemplate _footerTemplate;
        #endregion
        #region private int CurrentFilterType
        /// <summary>
        /// Gets the CurrentFilterType of the NewsList
        /// </summary>
        /// <value></value>
        public string CurrentFilterType
        {
            get
            {
                if (HttpContext.Current.Request.QueryString["page"] != null)
                {
                    string filter = HttpContext.Current.Request.QueryString["filter"];
                    if (string.IsNullOrEmpty(filter))
                        return filter;
                }
                return string.Empty;
            }
        }
        #endregion

        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region protected override void CreateChildControls()
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls()
        {

            IEnumerator e = base.GetPages().GetEnumerator();

            PageDataCollection pages = new PageDataCollection();

            while (e.MoveNext())
            {
                pages.Add(e.Current as PageData);
            }

            Control template = new EventPageTemplateContainer(null, null);
            // Abort if no data
            if (pages.Count == 0)
            {
                if (_emptyItemTemplate != null)
                {
                    _emptyItemTemplate.InstantiateIn(template);
                    Controls.Add(template);
                }
                return;
            }

            template = new EventPageTemplateContainer(null, null);
            if (_headerTemplate != null)
            {
                _headerTemplate.InstantiateIn(template);
                Controls.Add(template);
            }

            var curDate = DateTime.Now;// (DateTime)pages[0]["EventStart"] > DateTime.Now ? (DateTime)pages[0]["EventStart"] : DateTime.Now;
            curDate = new DateTime(curDate.Year, curDate.Month, curDate.Day, 0, 0, 0);
  
            
            var counter = 0;
            //int eventDayCounter = 0;
            int days = 0;
            if (HttpContext.Current.Request.QueryString["mode"] == "year")
                days = 365;
            else if (HttpContext.Current.Request.QueryString["mode"] == "month")
                days =30;
            else
                days = 7;
            //dynDiv.Attributes["class"] = "myCssClass";


            while (counter < days+1)
            {
                var events = pages.Where(p => (((DateTime)p["EventStart"]).Date <= curDate && ((DateTime)p["EventStop"]).Date >= curDate)).ToList();
                if (events.Count > 0)
                {
                    template = new EventPageTemplateContainer(events, curDate);

                    if (_eventHeadingTemplate != null)
                    {
                        _eventHeadingTemplate.InstantiateIn(template);
                        Controls.Add(template);

                    }
                    if (_itemTemplate != null)
                    {
                        _itemTemplate.InstantiateIn(template);
                        Controls.Add(template);
                    }

                    //eventDayCounter++;
                }
                curDate = curDate.AddDays(1);
                counter++;

            }

            if (_footerTemplate != null)
            {
                template = new EventPageTemplateContainer(null, null);
                _footerTemplate.InstantiateIn(template);
                Controls.Add(template);
            }
        }
        #endregion

        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
    }
}